package com.code.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.code.springboot.common.CustomException;
import com.code.springboot.dto.DishDto;
import com.code.springboot.entity.Dish;
import com.code.springboot.entity.DishFlavor;
import com.code.springboot.mapper.DishMapper;
import com.code.springboot.service.DishFlavorService;
import com.code.springboot.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: springboot
 * @create: 2022-08-10 11:46
 **/
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;



    //新增菜品，同时保存口味数据
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {

        //保存菜品信息到菜品表
        this.save(dishDto);

        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors.stream().map((item)->{
           item.setDishId(dishId);
           return item;
        }).collect(Collectors.toList());


        //保存口味信息到口味表
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询菜品对应口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());

        List<DishFlavor> flavors =dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新菜品信息


        this.updateById(dishDto);
        //清理口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //新增口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());


        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void updateStatus(int status, List<Long> ids) {
        //通过id查菜品
        List<Dish> dishes = this.listByIds(ids);
        //更新状态
        dishes.forEach(dish -> dish.setStatus(status));

        this.updateBatchById(dishes);

    }

    @Override
    public void removeWithFlavor(List<Long> ids) {
        //查询是否能删除
        //select from Dish id in (ids) and status = 1
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);

        int count = this.count(queryWrapper);

        //不能删除则抛异常
        if(count > 0){
            throw new CustomException("商品正在售卖。不能删除");
        }
        //能删除则删除当前表数据
        this.removeByIds(ids);
        //删除关联表数据
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper();

        lambdaQueryWrapper.in(DishFlavor::getDishId,ids);

        dishFlavorService.remove(lambdaQueryWrapper);
    }
}
